package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;
import java.util.UUID;

@Slf4j
@RestController
@RequestMapping("/img")
public class ImageGeneratorController {
    //基础字符
    private String words = "12asdf389qOPASDFyuiop47ghjklzxcvbnmQW0ERTYUIGHJKLZw56ertXCVBNM";
    //生成随机下标
    private Random random = new Random();
    @RequestMapping("/get")
    public void getImage(HttpServletResponse response) throws IOException {
        //验证码尺寸的大小
        int width = 158;
        int height = 40;
        //验证码文字个数
        int size = 4;
        //1.创建画板
        BufferedImage bufferedImage = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
        //2.获取笔触
        Graphics graphics = bufferedImage.getGraphics();
        //设置笔触颜色
        graphics.setColor(Color.lightGray);
        //使用笔触填充一个矩形画板
        //从左上角开始，填充width*height大小的矩形
        graphics.fillRect(0,0,width,height);

        //设置文本字体 宋体+加粗倾斜+大小20
        graphics.setFont(new Font("宋体",Font.BOLD+Font.ITALIC,40));
        //3.绘制验证码文字
        int wordsLength = words.length();
        //记录生成的验证码
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0;i<size;i++){
            //生成随机颜色，rgb范围是0-255
            graphics.setColor(
                    new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));
            //获取0-words长度的值
            int index = random.nextInt(wordsLength);
            char ch = words.charAt(index);
            stringBuffer.append(ch);
            //绘制验证码字符
            graphics.drawString(String.valueOf(ch),40+(i*30),30);

        }
        //4.绘制干扰线
        //设置干扰线颜色
        graphics.setColor(
                new Color(random.nextInt(256),random.nextInt(256),random.nextInt(256)));
        int lines = random.nextInt(10);
        for (int i = 0; i < lines; i++) {
            graphics.drawLine(random.nextInt(width),random.nextInt(height),
                    random.nextInt(width),random.nextInt(height));
        }
        log.info("生成的验证码:"+stringBuffer.toString());
        //获取响应流
        ServletOutputStream outputStream = response.getOutputStream();
        //输出 写到img的src属性里
        ImageIO.write(bufferedImage,"png",outputStream);
        //保存成文件
//        String imageName = UUID.randomUUID().toString()+".png";
//        File file = new File("src/main/resources/static/img/"+imageName);
//        ImageIO.write(bufferedImage, "png", file);
//        String imgname = "1aa66e96-2bda-408e-a7b2-2ac87bfcb6e7.png";
//        return AjaxResult.success(imageName,stringBuffer.toString());
    }
}
